package com.example.childpositionapp;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;

import com.example.childpositionapp.model.Location;
import com.example.childpositionapp.model.Login;
import com.example.childpositionapp.model.LoginReceive;
import com.example.childpositionapp.network.Tool;
import com.example.childpositionapp.network.Util;
import com.example.childpositionapp.util.UtilMd5;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {
    private Button bt;
    private EditText et;
    private EditText et_mm;
    private TextView tv_register;
    private TextView tv_changePsw;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        initView();
    }

    private void initView() {
        tv_register = findViewById(R.id.register);
        bt = findViewById(R.id.bt_login);
        et_mm = findViewById(R.id.et_mm);
        et = findViewById(R.id.et_zh);
        tv_changePsw=findViewById(R.id.change_psw);
        tv_changePsw.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this,ChangePwdActivity.class));
            }
        });
        et.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().length() < 1) {
                    bt.setEnabled(false);
                    bt.setBackgroundResource(R.drawable.shape);
                } else {
                    bt.setEnabled(true);
                    bt.setBackgroundResource(R.drawable.shape4);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });

        bt.setOnClickListener(new View.OnClickListener() {
            @RequiresApi(api = Build.VERSION_CODES.O)
            @Override
            public void onClick(View v) {
                if ("".equals(et.getText().toString())) {
                    Toast.makeText(LoginActivity.this, "邮箱不能为空！", Toast.LENGTH_SHORT).show();
                }else if ("".equals(et_mm.getText().toString())) {
                    Toast.makeText(LoginActivity.this, "密码不能为空！", Toast.LENGTH_SHORT).show();
                }else {
                String s = et.getText().toString();
                String mm = et_mm.getText().toString();
                String str = new Gson().toJson(new Login(s, UtilMd5.stringMD5(mm)));
                sendLogin(str);
                }
            }
        });

        tv_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            }
        });
    }

    private void sendLogin(String str) {
        Tool.Login(str, new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(LoginActivity.this, "连接失败", Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                LoginReceive receive = new Gson().fromJson(response.body().string(), LoginReceive.class);
                runOnUiThread(new Runnable() {
                    @SuppressLint("LongLogTag")
                    @Override
                    public void run() {
                        if (receive.getCode() == 200) {
                            Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
                            Util.setToken(receive.getData().getToken());
                            Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                            startActivity(intent);
                            finish();
                        } else {
                            Toast.makeText(LoginActivity.this, receive.getMessage(), Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        });
    }
}